package cn.life.business.project.service.impl;

import cn.life.business.project.dao.ProjectDao;
import cn.life.business.project.entity.po.ProjectPo;
import cn.life.business.project.entity.vo.ProjectVo;
import cn.life.business.project.service.ProjectService;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.common.entity.model.CMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:    项目Service实现层
 *
 * @Author 李佳明
 * @Date 2018-07-24 15:56
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectDao projectDao;

    @Override
    public CMessage addProject(ProjectPo projectPo) {
        return projectDao.addProject(projectPo) != null ? CMessage.addSuccess() : CMessage.addFail();
    }

    @Override
    public CMessage updateProject(ProjectPo projectPo) {
        return projectDao.updateProject(projectPo) == 1 ? CMessage.modifySuccess() : CMessage.modifyFail();
    }

    @Override
    public CMessage deleteProject(ProjectPo projectPo) {
        return projectDao.deleteProject(projectPo) == 1 ? CMessage.removeSuccess() : CMessage.removeFail();
    }

    @Override
    public CMessage queryProjectList(ProjectPo projectPo) {
        return CMessage.success("查询成功",projectDao.queryProjectList(projectPo));
    }

    @Override
    public CMessage queryProjectDetail(ProjectPo projectPo) {
        return CMessage.success("查询成功",projectDao.queryProjectDetail(projectPo));
    }

    @Override
    public CMessage queryProjectDetails(ProjectPo projectPo) {
        //1.项目详情信息
        ProjectVo projectVo = new ProjectVo();
        projectPo =  projectDao.queryProjectDetail(projectPo);
        BeanUtils.copyProperties(projectPo,projectVo);
        Long nowTime = new Date().getTime();
        if(projectVo.getStartDate().getTime() < nowTime){
            projectVo.setProjectState(0);
            projectVo.setProjectStateName("筹备");
        }else if(projectVo.getStartDate().getTime() >= nowTime && nowTime <= projectVo.getEndDate().getTime()){
            projectVo.setProjectState(1);
            projectVo.setProjectStateName("在建");
        }else{
            projectVo.setProjectState(2);
            projectVo.setProjectStateName("完毕");
        }
        //2.项目人员信息
        List<UserInfo> userInfoList = projectDao.getProjectUserList(projectPo);
        projectVo.setUserList(userInfoList);
        return CMessage.successByData(projectVo);
    }
}
